import request from '@/utils/request'
import { getChannels, hasToken, setChannels } from '@/utils/storage'
import {
  SAVEUSERCHANNELS,
  SETALLCHANNELS,
  SETARTICLELIST,
  SETMOREACTION,
} from '../action_types/home'

// 获取频道数据
export const getUserchannels = () => {
  return async (dispatch) => {
    if (hasToken()) {
      const res = await request('/user/channels')
      dispatch(saveUserchannels(res.data.channels))
    } else {
      const channels = getChannels()
      if (channels) {
        dispatch(saveUserchannels(channels))
      } else {
        const res = await request('/user/channels')
        dispatch(saveUserchannels(res.data.channels))
        setChannels(res.data.channels)
      }
    }
  }
}

// 保存用户频道
export const saveUserchannels = (payload) => {
  return {
    type: SAVEUSERCHANNELS,
    payload,
  }
}

// 全部用户频道
export const allUserchannels = () => {
  return async (dispatch) => {
    const res = await request.get('/channels')
    dispatch(setAllChannels(res.data.channels))
  }
}

// 保存所有用户频道
export const setAllChannels = (payload) => {
  return {
    type: SETALLCHANNELS,
    payload,
  }
}

// 删除频道
export const delChannels = (channel) => {
  return async (dispatch, getState) => {
    const Userchannels = getState().home.Userchannels
    if (hasToken()) {
      await request.delete(`/user/channels/${channel.id}`)
      dispatch(
        saveUserchannels(Userchannels.filter((item) => item.id !== channel.id))
      )
    } else {
      const channels = Userchannels.filter((item) => item.id !== channel.id)
      dispatch(saveUserchannels(channels))
      setChannels(channels)
    }
  }
}

// 添加频道
export const addChannel = (channel) => {
  return async (dispatch, getState) => {
    const channels = [...getState().home.Userchannels, channel]
    if (hasToken()) {
      await request.patch('/user/channels', {
        channels: [channel],
      })
      dispatch(saveUserchannels(channels))
    } else {
      dispatch(saveUserchannels(channels))
      setChannels(channels)
    }
  }
}

// 获取频道列表
export const getArticleList = (channelId, timestamp, loa = false) => {
  return async (dispatch) => {
    const res = await request.get('/articles', {
      params: {
        channel_id: channelId,
        timestamp: timestamp,
      },
    })
    dispatch(
      setArticleList({
        channelId,
        timestamp: res.data.pre_timestamp,
        list: res.data.results,
        loa,
      })
    )
  }
}

// 保存频道列表到redux
export const setArticleList = (payload) => {
  return {
    type: SETARTICLELIST,
    payload,
  }
}

// 图片懒加载
export const setMoreAction = (payload) => {
  return {
    type: SETMOREACTION,
    payload,
  }
}

export const unLinkArticle = (articleId) => {
  return async (dispatch, getState) => {
    await request({
      method: 'post',
      url: '/article/dislikes',
      data: {
        target: articleId,
      },
    })
    // 把当前频道对应的文章删除
    const channelId = getState().home.moreaction.channelId
    const articles = getState().home.articles[channelId]
    dispatch(
      setArticleList({
        channelId,
        timestamp: articles.timestamp,
        list: articles.list.filter((item) => item.art_id !== articleId),
      })
    )
  }
}

export const reportArticle = (articleId, reportId) => {
  return async (dispatch, getState) => {
    await request({
      method: 'post',
      url: '/article/reports',
      data: {
        target: articleId,
        type: reportId,
      },
    })
    // 把当前频道对应的文章删除
    const channelId = getState().home.moreaction.channelId
    const articles = getState().home.articles[channelId]
    console.log(articles)
    dispatch(
      setArticleList({
        channelId,
        timestamp: articles.timestamp,
        list: articles.list.filter((item) => item.art_id !== articleId),
      })
    )
  }
}
